package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.entity.YddLink;
import com.itheima.ydd.service.IYddLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ydd_link")
public class YddLinkController {
    @Autowired
    private IYddLinkService yddLinkService;




    /**
     * 分页查询链接
     *
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String type, int page, int limit) {
        //构造分页构造器对象
        Page<YddLink> pageInfo = new Page<>(page, limit);
        //条件构造器
        LambdaQueryWrapper<YddLink> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.eq(YddLink::getType, type);
        //添加排序条件条件
        queryWrapper.orderByAsc(YddLink::getId);
        //执行查询
        yddLinkService.page(pageInfo, queryWrapper);

        List<YddLink> linkList = pageInfo.getRecords();
        if ("1".equals(type)) {
            for (YddLink yddLink : linkList) {
                yddLink.setType("文字类型");
            }
        } else if ("2".equals(type)) {
            for (YddLink yddLink : linkList) {
                yddLink.setType("图片类型");
            }

        }

        PageDto pageDto = new PageDto();
        pageDto.setPageSize(limit);
        pageDto.setCurrentPage(page);
        pageDto.setData(linkList);
        pageDto.setCount((int)pageInfo.getTotal());
        pageDto.setTotalPages((int)pageInfo.getPages());




     return ResultData.ok("",pageDto);
    }

    /**
     * 根据id查询 修改01 数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(String id) {

        LambdaQueryWrapper<YddLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddLink::getId, id);
        YddLink link   = yddLinkService.getOne(queryWrapper);
        //  = yddLinkService.getById(id);
       return ResultData.ok("",link);

    }

    /**
     * 修该02
     * @param yddLink
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddLink yddLink) {
        yddLinkService.updateById(yddLink);
        return R.success("修改成功","");
    }

    /**
     * 删除链接
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(String id){
        YddLink yddLink = yddLinkService.getById(id);
        if("1".equals(yddLink.getDefaultData())){
            return R.error("系统默认数据,不能删除");
        }
        yddLinkService.removeById(id);

        return R.success("删除成功","");
    }

    /**
     * 添加链接
     * @param yddLink
     * @return
     */

    @PostMapping("/add")
    public R<String> add(@RequestBody YddLink yddLink){

        yddLinkService.saveOrUpdate(yddLink);
        return R.success("添加成功","");
    }
}
